import { request, downloadBlobFile } from 'src/utils';

export interface ConfigDto {
  configId: IdType; // 参数ID
  configName: string; // 参数名称
  configKey: string; // 参数编码
  configValue: string; // 参数值
  remark: string; // 备注说明
  updatedBy: string; // 修改人
  updatedTime: Date; // 修改时间
}

const baseUrl = '/api/system/config';

/**
 * 查单个
 */
export function readData(configId: IdType): Promise<ConfigDto> {
  return request.get(baseUrl + '/' + configId);
}

/**
 * 查集合
 */
export function queryList(query: PageQuery): Promise<PageResult<ConfigDto>> {
  return request.get(baseUrl, { params: query });
}

/**
 * 新增，修改
 */
export function saveOrUpdate(config: ConfigDto): Promise<ConfigDto> {
  return config.configId ? request.put(baseUrl + '/' + config.configId, config) : request.post(baseUrl, config);
}

/**
 * 删除
 */
export function deleteByIds(ids: IdType[]): Promise<void> {
  return request.delete(baseUrl, { params: { ids } });
}

/**
 * 导出
 */
export function exportData(query: PageQuery): Promise<void> {
  return new Promise((resolve, reject) => {
    request
      .get(baseUrl + '/export', { params: query, responseType: 'blob' })
      .then((res) => {
        downloadBlobFile(res);
        resolve();
      })
      .catch((err) => reject(err));
  });
}

export default { readData, queryList, saveOrUpdate, deleteByIds, exportData } as CrudApi<ConfigDto>;
